<template>
    <div class="roles">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="IsRoles = true">添加用户</el-button>
                </el-col>
            </el-row>
<!--            主页面-->
            <el-table :data="rolesList" border stripe>
                <el-table-column type="expand">
                    <template v-slot="slot">
                        <el-row v-for="(item1,il) in slot.row.children" :key="item1.id" :class="['border_bottom',il===0? 'border_top':'','v_center']">
                            <el-col :span="5">
                                <el-tag closable @close="removeRolesTag(slot.row,item1.id)">{{item1.authName}}</el-tag>
                                <i class="el-icon-caret-right"></i>
                            </el-col>
                            <el-col :span="19">
                                <el-row v-for="(item2,i2) in item1.children" :key="item2.id" :class="[i2 === 0? '':'border_top','v_center']">
                                    <el-col :span="6">
                                        <el-tag type="success" closable @close="removeRolesTag(slot.row,item2.id)">{{item2.authName}}</el-tag>
                                        <i class="el-icon-caret-right"></i>
                                    </el-col>
                                    <el-col :span="18">
                                        <el-tag type="warning" v-for="(item3,i3) in item2.children" :key="item3.id"
                                        closable @close="removeRolesTag(slot.row,item3.id)">{{item3.authName}}</el-tag>
                                    </el-col>
                                </el-row>
                            </el-col>
                        </el-row>
                    </template>
                </el-table-column>
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column label="角色名称" prop="roleName"></el-table-column>
                <el-table-column label="角色描述" prop="roleDesc"></el-table-column>
                <el-table-column label="操作" width="300">
                    <template v-slot="slot">
                        <el-button size="mini" type="primary" icon="el-icon-edit">修改</el-button>
                        <el-button size="mini" type="danger" icon="el-icon-delete" @click="removeUser(slot.row.id)">删除</el-button>
                        <el-button size="mini" type="warning" icon="el-icon-setting" @click="showSetRightDialog(slot.row)">分配</el-button>
                    </template>
                </el-table-column>
            </el-table>
<!--            添加角色-->
            <el-dialog
                    title="添加角色"
                    :visible.sync="IsRoles"
                    width="50%"
            @close="rolesDialogClose">
                <el-form ref="rolesFormRef" label-width="70px" :model="rolesForm" :rules="rolesFormRules">
                    <el-form-item label="角色名称" prop="roleName" label-width="15%">
                        <el-input v-model="rolesForm.roleName"></el-input>
                    </el-form-item>
                    <el-form-item label="角色描述" prop="roleDesc" label-width="15%">
                        <el-input v-model="rolesForm.roleDesc"></el-input>
                    </el-form-item>
                </el-form>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="IsRoles = false" >取 消</el-button>
                    <el-button type="primary" @click="rolesUserInfo">确 定</el-button>
                </span>
            </el-dialog>
        </el-card>
<!--        分配角色的权限列表-->
        <el-dialog
                title="设置权限"
                :visible.sync="setdialogRoles"
                width="30%"
                >
            <el-tree :data="powerList" :props="treeProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="tree_keys" ref="treeRef"/>
            <span slot="footer" class="dialog-footer">
                <el-button @click="setdialogRoles = false">取 消</el-button>
                <el-button type="primary" @click="allotRights">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "roles",
        data(){
            return{
                rolesList:[],
                rolesForm:{},
                IsRoles:false,
                powerList:[],
                setdialogRoles:false,
                tree_keys:[],
                rolesFormRules:{
                    roleName:[
                        { required: true, message: '请输入角色信息', trigger: 'blur' },
                        { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
                    ],
                    roleDesc:[
                        { required: true, message: '请输入描述信息', trigger: 'blur' },
                        { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger:'blur' }
                    ]
                },
                treeProps:{
                    children:"children",
                    label:"authName"
                },
                roleId:''
            }
        },
        created() {
            this.getRolesList();
        },
        methods:{
            async getRolesList(){
                const {data:res}=await this.$http.get("roles");
                if(res.meta.status!==200){
                    return this.$message.error("数据获取失败");
                }
                this.rolesList=res.data;
            },
            async removeUser(id){
                const confirmResult=await this.$confirm("此操作将永久删除该用户，是否继续？","提示",{
                    confirmButtonText:"确定",
                    cancelButtonText:"取消",
                    type:'warning'
                }).catch(err=>err);
                if(confirmResult!=="confirm"){
                    return this.$message.info("已经取消删除");
                }
                else {
                    const {data:res}=await this.$http.delete("roles/"+id);
                    if(res.meta.status!==200){
                        this.$message.error("删除失败");
                        return
                    }
                    this.$message.success("删除成功");
                    this.getRolesList();
                    // console.log(res);
                }
            },
            rolesDialogClose(){
                this.$refs.rolesFormRef.resetFields();
                this.$refs.rolesFormRef.clearValidate();
            },
            rolesUserInfo(){
                this.$refs.rolesFormRef.validate(vali=>{
                    if(!vali) return;
                    this.$http.post("roles",this.rolesForm).then(res=>{
                        if(res.data.meta.status!==201){
                            this.$message.error("添加失败");
                            return
                        }
                        this.$message.success("添加成功");
                        this.IsRoles=false;
                        this.getRolesList();
                    })
                })
            },
            async removeRolesTag(row,id){
               const resultConfirm=await this.$confirm('此操作将永久删除该标签, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).catch(err=>err);
                if(resultConfirm !=="confirm"){
                    return this.$message.info("取消删除")
                }
                var row_id=parseInt(row.id);
                const {data:res}=await this.$http.delete(`roles/${row_id}/rights/${id}`);
                // console.log(res);
                if(res.meta.status!==200){
                    return this.$message.error("删除失败")
                }
                // this.getRolesList();
                row.children=res.data;
            },
            async showSetRightDialog(role){
                this.roleId=role.id;
                const {data:res}=await this.$http.get("rights/tree");
                this.tree_keys.splice(0,this.tree_keys.length);
                if(res.meta.status!==200){
                    return this.$message.error("请求权限失败");
                }
                this.powerList=res.data;
                this.getLeafKeys(role,this.tree_keys);
                // console.log(res);
                this.setdialogRoles=true;

            },
            getLeafKeys(node,arr){
                if(!node.children){
                    return arr.push(node.id)
                }
                node.children.forEach(item=>{
                    this.getLeafKeys(item,arr);
                })
            },
           async allotRights(){
                const keys=[...this.$refs.treeRef.getCheckedKeys(),...this.$refs.treeRef.getHalfCheckedKeys()];
                // console.log(keys)
                const idStr=keys.join(',');
                const {data:res}=await this.$http.post(`roles/${this.roleId}/rights`,{rids:idStr});
                if(res.meta.status!==200){
                    return this.$message.error("更新权限失败")
                }
                this.$message.success("更新权限成功");
                this.getRolesList();
                this.setdialogRoles=false;
            }

        }
    }
</script>

<style lang="less" scoped>
    .roles{
        padding: 15px;
    }
    .el-card{
        margin-top: 15px;
    }
    .el-tag{
        margin: 7px;
    }
    .border_bottom{
        border-bottom: 1px solid #eeeeee;
    }
    .border_top{
        border-top: 1px solid #eee;
    }
    .v_center{
        display: flex;
        align-items: center;
    }
    [type=expand]{
        transition: 0.5s linear;
    }
</style>
